package com.jinlejia.erp.ui.house;

import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSONObject;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.FragmentPropertyBinding;
import com.jinlejia.erp.utils.DateUtil;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

/**
 * 属性信息
 */
public class PropertyFragment extends Fragment {

    private FragmentPropertyBinding binding;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_property, container, false);
        binding = DataBindingUtil.bind(view);
        EventBus.getDefault().register(this);
        return view;
    }

    @Subscriber(tag = "TAG_HOUSE_DATA")
    public void houseData(JSONObject data) {
        parseData(data);
    }

    private void parseData(JSONObject data) {
        System.out.println(data.toString());
        binding.area.setText(TextUtils.isEmpty(data.getString("square")) ? "/" : data.getString("square"));
        binding.taoneiArea.setText(TextUtils.isEmpty(data.getString("setInSquare")) ? "/" : data.getString("setInSquare"));
        if (!data.containsKey("isLoad")) {
            binding.kedaikuan.setText("/");
        } else if (data.get("isLoad").toString().equals("0")) {
            binding.kedaikuan.setText("否");
        } else if (data.get("isLoad").toString().equals("1")) {
            binding.kedaikuan.setText("是");
        }

        binding.yuanjia.setText(TextUtils.isEmpty(data.getString("orgnalPrice")) ? "/" : data.getString("orgnalPrice"));
        binding.chanquanren.setText(TextUtils.isEmpty(data.getString("propertyOwner")) ? "/" : data.getString("propertyOwner"));
        if (TextUtils.isEmpty(data.getString("houseStatus"))){
            binding.fangwuxianzhaung.setText("/");
        }else{
            if (data.getString("houseStatus").contains("金额仅供参考不参与分佣")) {
                if (data.getString("houseStatus").contains("套")){
                    String houseStatus = data.getString("houseStatus");
                    String strStart = houseStatus.substring(0, houseStatus.indexOf("万")+1);
                    String str1 = houseStatus.substring(0, houseStatus.indexOf(")"));
                    String endstring = houseStatus.substring(str1.length() + 1);
                    Spanned html = Html.fromHtml(strStart+"(&nbsp;<font color='#FF0000'>"+"金额仅供参考不参与分佣"+"</font>&nbsp;)"+endstring);
                    binding.fangwuxianzhaung.setText(html);
                }else{
                    String houseStatus = data.getString("houseStatus");
                    String strStart = houseStatus.substring(0, houseStatus.indexOf("万")+1);
                    Spanned html = Html.fromHtml(strStart+"(&nbsp;<font color='#FF0000'>"+"金额仅供参考不参与分佣"+"</font>&nbsp;)");
                    binding.fangwuxianzhaung.setText(html);
                }
            }else{
                binding.fangwuxianzhaung.setText(data.getString("houseStatus"));
            }
        }
        binding.jiaofangTime.setText(TextUtils.isEmpty(data.getString("transferTime")) ? "/" : DateUtil.getTime(data.getString("transferTime"), "yyyy-MM-dd"));

        if( !data.get("type").toString().equals("shs") ){
            binding.layoutDaikuan.setVisibility(View.GONE);
            binding.layoutYuangoujia.setVisibility(View.GONE);
            binding.layoutChanquanren.setVisibility(View.GONE);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
